package com.example.gamecard

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModelProvider
import com.example.gamecard.ui.theme.GameCardTheme

class GameCardActivity : ComponentActivity() {
    private lateinit var viewModel: GameCardViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProvider(this)[GameCardViewModel::class.java]
        setContent {
            GameCardTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    GameCard(
                        modifier = Modifier.padding(innerPadding),
                        viewModel = viewModel
                    )
                }
            }
        }
    }
}

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun GameCard(modifier: Modifier = Modifier, viewModel: GameCardViewModel? = null) {
    Column(
        modifier = modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .verticalScroll(ScrollState(0)),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Top
    ) {
        Text(
            text = "卡牌游戏",
            style = MaterialTheme.typography.titleLarge
        )
        Button(onClick = {
            viewModel?.startGame()
        }) {
            Text(text = "进行游戏")
        }
        if (!viewModel?.winner.isNullOrEmpty()) {
            Spacer(modifier = modifier.height(14.dp))
            Text(
                text = "获胜玩家",
                style = MaterialTheme.typography.titleLarge
            )
            Card {
                Box(
                    modifier = Modifier
                        .padding(8.dp)
                        .align(Alignment.CenterHorizontally)
                ) {
                    Text(text = viewModel?.winner ?: "")
                }
            }
        }
        if (!viewModel?.shuffledCards.isNullOrEmpty()) {
            Spacer(modifier = modifier.height(14.dp))
            Text(
                text = "最新排序",
                style = MaterialTheme.typography.titleLarge
            )
            FlowRow(
                horizontalArrangement = Arrangement.spacedBy(8.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                viewModel?.shuffledCards?.forEach { card ->
                    Card {
                        Box(
                            modifier = Modifier
                                .padding(8.dp)
                                .align(Alignment.CenterHorizontally)
                        ) {
                            Text(text = card)
                        }
                    }
                }
            }
        }
        if (!viewModel?.players.isNullOrEmpty()) {
            Spacer(modifier = modifier.height(14.dp))
            Text(
                text = "玩家手牌:",
                style = MaterialTheme.typography.titleLarge
            )
            viewModel?.players?.forEachIndexed { index, player ->
                Card {
                    Box(
                        modifier = Modifier
                            .padding(8.dp)
                            .align(Alignment.CenterHorizontally)
                    ) {
                        Text(text = "玩家 ${index + 1}:")
                    }
                }
                FlowRow(
                    horizontalArrangement = Arrangement.spacedBy(8.dp),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    player.cards.forEach { card ->
                        Card {
                            Box(
                                modifier = Modifier
                                    .padding(8.dp)
                                    .align(Alignment.CenterHorizontally)
                            ) {
                                Text(text = card)
                            }
                        }
                    }
                }
                if (index < viewModel.players.size - 1) {
                    Spacer(modifier = modifier.height(14.dp))
                }
            }
        }
    }
}

@Preview(showBackground = true, device = "spec:width=1080px,height=1920px,dpi=480")
@Composable
fun GameCardPreview() {
    GameCardTheme {
        GameCard()
    }
}